/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : join_room.js                                 
*  Description  : 조인룸 리스트                                       
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-04-03  김효순    Created.  
*  2012-04-04  김효순    검색바 추가          
*******************************************************************************/

jQuery(function($){
	// 지역리스트
	function clsAreaList()
	{
		this._HTMLDom = null;
		this.listData = null;
		this.idx = null;
		this.dispName = null;
		this.keyName = null;
		
		this._init();
	}
	
	clsAreaList.prototype = {
		_init: function(){
			var $layer = $("article.all_joinroom_search>div.selectlayer:first");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("ul:first>li:first").clone(true)
			}
			
			this.keyName = "지역코드";
			this.dispName = "지역명";
			
			this.setList({
				"지역코드": ""
				, "지역명": "지역선택"
			});
			this.setIDX(0);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){
			if(!this.listData) this.listData = [];
			this.listData = this.listData.concat(thisData);
			this.display();
		}
		, display: function(){
			var $layer = this.$getLayer();
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("ul:first").empty();
			for(var i=0;i<this.listData.length;i++){
				$layer.find("ul:first").append($element.clone(true));
			}
			$layer.find("ul:first>li a").each(function(n){
				$(this).html(thisClass.listData[n][thisClass.dispName]);
			});
		}
		, setIDX: function(idx){
			var $layer = this.$getLayer();
			this.idx = idx;
			
			$layer.find("ul:first").hide();
			this.displayTitle();
		}
		, getKey: function(){
			return this.listData[this.idx][this.keyName];
		}
		, setKey: function(key){
			for(var i=0;i<this.listData.length;i++){
				if(this.listData[i][this.keyName] == key) break;
			}
			
			if(i<this.listData.length) this.setIDX(i);
		}
		, displayTitle: function(){
			var $layer = this.$getLayer();
			
			$layer.find("p.default:first>a").html(this.listData[this.idx][this.dispName]);
		}
		, toggle: function(){
			var $layer = this.$getLayer();
			var $obj = $layer.find("ul:first");
			
			if($obj.is(":visible")){
				this.hide();
			}else if($obj.is(":hidden")){
				this.show();
			}
		}
		, show: function(){
			this.$getLayer().find("ul:first").show(300);
		}
		, hide: function(){
			this.$getLayer().find("ul:first").hide();
		}
	}
	
	// 기간별 조회(시작)
	function clsSDate()
	{
		this._HTMLDom = null;
		this.date = null;
		
		this._init();
	}
	
	clsSDate.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.all_joinroom_date:eq(0)");
			var $calendar = $("div#container section#main_content div.selectdate div#idDate1");
			var thisClass = this;
			
			this._HTMLDom = {
				"$layer": $layer
				, "$calendar": $calendar
			}
			
			$calendar.css("margin-left", "110px").datepicker({
				onSelect: function(dateText, inst){
					thisClass.date = dateText;
					thisClass.display();
					thisClass.hide();
				}
			});
			
			this.show();
			this.hide();
			this.display();
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, show: function(){
			var $calendar = this._HTMLDom["$calendar"];
			
			$calendar.parent("div.selectdate").children(":not(span.bt_close)").hide();
			$calendar.parent("div.selectdate").show();
			$calendar.show(300);
		}
		, hide: function(){
			this._HTMLDom["$calendar"].hide();
			this._HTMLDom["$calendar"].parent("div.selectdate").hide();
		}
		, display: function(){
			this.$getLayer().html(this.date ? this.date:"기간선택");
		}
		, getValue: function(){
			if(this.date) return this.date;
			else return "";
		}
		, setValue: function(val){
			this.date = val;
			this.display();
		}
	}
	
	// 기간별 조회(종료)
	function clsEDate()
	{
		this._HTMLDom = null;
		this.date = null;
		
		this._init();
	}
	
	clsEDate.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content div.all_joinroom_date:eq(1)");
			var $calendar = $("div#container section#main_content div.selectdate div#idDate2");
			var thisClass = this;
			
			this._HTMLDom = {
				"$layer": $layer
				, "$calendar": $calendar
			}
			
			$calendar.css("margin-left", "200px").datepicker({
				onSelect: function(dateText, inst){
					thisClass.date = dateText;
					thisClass.display();
					thisClass.hide();
				}
			});
			
			this.show();
			this.hide();
			this.display();
		}
		, $getLayer: clsSDate.prototype.$getLayer
		, show: clsSDate.prototype.show
		, hide: clsSDate.prototype.hide
		, display: clsSDate.prototype.display
		, getValue:clsSDate.prototype.getValue
		, setValue: clsSDate.prototype.setValue
	}

	
	
	// 기간별 조회
	function clsPeriod()
	{
		this._HTMLDom = null;
		this.date = null;
		this.calendarLock = false;
		
		this._init();
	}
	
	clsPeriod.prototype = {
		_init: function(){
			var thisClass = this;
			
			this._HTMLDom = {
				"$clickLayer": $("article.all_joinroom_search>div.all_joinroom_date:first")
				, "$showLayer":$("article.all_joinroom_search>div.selectdate:first")
				, "$displayLayer": $("article.all_joinroom_search>div.all_joinroom_date:first")
				, "$closeLayer": $("article.all_joinroom_search>div.selectdate:first>.bt_close")
			}
			
			this.date = {
				"sdate": ""
				, "edate": ""
			}
			
			// 달력객체 생성
			$("#idDate1, #idDate2").datepicker({
				onSelect: function(dateText, inst){
					
					switch(this.id){
						case "idDate1":
							thisClass.setDate("sdate", dateText);
							break;
						case "idDate2":
							thisClass.setDate("edate", dateText);
							break;
					}
					thisClass.displayTitle();
				}
				, onChangeMonthYear: function(year, month, inst){
					if(thisClass.getCalendarLock()) return;
					
					thisClass.setCalendarLock(true);
					
					var newDate = new Date(year, month-1, 1);
					
					switch(this.id){
						case "idDate1":
							newDate.setMonth(newDate.getMonth()+1);
							$("#idDate2").datepicker("setDate", newDate.getFullYear() + "-" + (newDate.getMonth()+1) + "-01");
							break;
						case "idDate2":
							newDate.setMonth(newDate.getMonth()-1);
							$("#idDate1").datepicker("setDate", newDate.getFullYear() + "-" + (newDate.getMonth()+1) + "-01");
							break;
					}
					
					thisClass.setCalendarLock(false);
				}
			});
			
			// 두번째 달력은 한달뒤에껄로 세팅
			$("#idDate2").datepicker("setDate", (new Date().getFullYear()) + "-" + (new Date().getMonth()+2) + "-01");
			
			this.displayTitle();
		}
		, show: function(){
			this._HTMLDom["$showLayer"].show(300);
		}
		, hide: function(){
			this._HTMLDom["$showLayer"].hide();
		}
		, displayTitle: function(){
			var szDisp = (!(this.date["sdate"] || this.date["edate"])) ? "기간대 조회":this.date["sdate"] + "~" + this.date["edate"];
			
			this._HTMLDom["$displayLayer"].html(szDisp);
		}
		, getDate: function()
		{
			if(this.getDate.arguments.length > 0){
				return this.date[this.getDate.arguments[0]];
			}else{
				return this.date;
			}
		}
		, setDate: function(key, val){
			this.date[key] = val;
		}
		, getCalendarLock: function(){
			return this.calendarLock;
		}
		, setCalendarLock: function(lock){
			this.calendarLock = lock;
		}
		, $getClick: function(){
			return this._HTMLDom["$clickLayer"];
		}
		, $getClose: function(){
			return this._HTMLDom["$closeLayer"];
		}
	}
	
	//  시간대 조회
	function clsTime()
	{
		this._HTMLDom = null;
		this.listData = null;
		this.idx = null;
		this.dispName = null;
		this.keyName = null;
		
		this._init();
	}
	
	clsTime.prototype = {
		_init: function(){
			var $layer = $("article.all_joinroom_search>.selectlayer:eq(1)")
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("ul:first>li:first").clone(true)
			}
			
			this.keyName = "키";
			this.dispName = "내용";
			
			this.setList({
				"키": ""
				, "내용": "시간대조회"
			});
			this.setIDX(0);
		}
		, $getLayer: clsAreaList.prototype.$getLayer
		, setList: clsAreaList.prototype.setList
		, display: clsAreaList.prototype.display
		, displayTitle:clsAreaList.prototype.displayTitle
		, setIDX: clsAreaList.prototype.setIDX
		, getKey: clsAreaList.prototype.getKey
		, setKey: clsAreaList.prototype.setKey
		, toggle: clsAreaList.prototype.toggle
		, show: clsAreaList.prototype.show
		, hide: clsAreaList.prototype.hide
	}
	
	// 등록일, 라운드날짜순
	function clsJoinroomOrder()
	{
		this._HTMLDom = null;
		this._listData = null;
		this.idx = null;
		
		this._init();
	}
	
	clsJoinroomOrder.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.joinroom_list p.list_order")
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this._listData = ["0001", "0002"];
			
			this.setIdx(0);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setIdx: function(n){
			this.idx = n;
			this.display();
		}
		, getKey: function(){
			return this._listData[this.idx];
		}
		, display: function(){
			var $layer = this.$getLayer();
			
			$layer.find("a").removeClass("on");
			$layer.find("a").eq(this.idx).addClass("on");
		}
	}
	
	// 조인룸 리스트
	function clsJoinroomList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsJoinroomList.prototype = {
		_init: function(){
			var $layer = $("article.joinroom_list");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("dl:first").clone(true)
				, "$noresult": $layer.prev("article.joinroom_list_none")
			}
			
			this.setList([]);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, setWriterInfo: function(n, infoData){
			this.listData[n]["작성자"] = infoData;
			this.displayWriter(n);
		}
		, setJoinList: function(key, thisData){
			for(var i=0;i<this.listData.length;i++){
				if(this.listData[i]["키"]+"" == key+"") break;
			}
			
			if(i < this.listData.length){
				this.listData[i]["참여자"] = thisData;
				this.displayJoin(i);
			}
		}
		, setApplyList: function(joinroomKey, thisData){
			for(var i=0;i<this.listData.length;i++){
				if(this.listData[i]["키"]+"" == joinroomKey+"") break;
			}
			
			if(i < this.listData.length){
				this.listData[i]["신청자"] = thisData;
				this.displayApply(i);
			}
		}
		, display: function(){
			var thisClass = this;
			var $layer = this.$getLayer();
			var $element = this._HTMLDom["$element"];
			var $noresult = this._HTMLDom["$noresult"];
			
			var bName = {
				"C": "진행중"
				, "D": "조인마감"
				, "E": "조인완료"
			}
			
			if(this.listData.length){
				$layer.show();
				$noresult.hide();
				
				$layer.children("dl").remove();
				for(var i=0;i<this.listData.length;i++){
					$layer.append($element.clone(true));
				}
				
				$layer.children("dl").each(function(n){
					var d = thisClass.listData[n];
					
					$(this).find("#thumb01:first").attr("src", _gf.getDefaultProfileImage());
					if(d["비밀방"]) $(this).find("p.lock:first img").show();
					else $(this).find("p.lock:first img").hide();
					$(this).find(".time:first>a:first").html(d["시간"]);
					$(this).find(".id:first").html(d["골프장코스"]);
					$(this).find(".join_cor:first").html(d["홀명"] + "H");
					$(this).find(".nickname>a:first").html(d["작성자"]["닉네임"]);
					$(this).find("div.room_info p.time a").contents().filter(function(){
						return (this.nodeType==3 && this.data.trim());
					}).before(d["부킹일"] + "(" + d["요일"] + ") ").remove();
					$(this).find("div.room_info p.time span").html(d["부킹시"]);
					$(this).find(".offer>span:first").html(0);
					$(this).find("div.name_cont p.sex").html("");
					//$(this).find("div.name_cont p.sex span:eq(0)").html("");
					//$(this).find("div.name_cont p.sex span:eq(1)").html(d["작성자"]["나이대"]);
//					$(this).find("div.name_cont p.sex").contents().filter(function(){
//						return (this.nodeType == 3 && this.data.trim() && this.data.trim() != "/");
//					}).before(" / " + d["작성자"]["지역"]).remove();
					$(this).find("div.room_txt:first").html(d["내용"]);
					$(this).find("ol.person").empty();
					for(var i=0;i<d["초대인원"];i++){
						$(this).find("ol.person").append("<li class='off'>" + (i+1) + "</li>\n");
					}
					
					$(this).find("div.room_info p.condi img").hide().filter(function(){
						if($(this).attr("alt") == bName[d["조인룸상태코드"]]) $(this).show();
					});
				});
			}else{
				$layer.hide();
				$noresult.show();
			}
		}
		, displayWriter: function(n){
			var $layer = this.$getLayer();
			var thisClass = this;
			
			var genderC = {
				"M": "sex_m"
				, "F": "sex_w"
			}
			
			var d = this.listData[n];
			
			var ageCode = d["작성자"]["나이대"] || "없음";
			var areaCode = d["작성자"]["지역"] || "없음";
			
			$layer.find("dl:eq(" + n + ") #thumb01:first").attr("src", d["작성자"]["이미지"]).error(function(){
					$(this).attr("src", _gf.getDefaultProfileImage());
			});
			$layer.find("dl:eq(" + n + ") .nickname>a:first").html(d["작성자"]["닉네임"]).hover(function(){
				_gf.showProfileLayer($(this), d["작성자"], "left", 0, $(this).height());
			}, function(){
				_gf.closeProfileLayer();
			});
			$layer.find("dl:eq(" + n + ") div.name_cont p.sex").html("<span class='" + genderC[d["작성자"]["성별코드"]] + "'>" + d["작성자"]["성별"] + "</span> / <span class='age'>" + d["작성자"]["나이대"] + "</span> / " + d["작성자"]["지역명"]);
			
//			$layer.find("dl:eq(" + n + ") div.name_cont p.sex span:eq(0)").attr("class", genderC[d["작성자"]["성별코드"]]).html(d["작성자"]["성별"]);
//			$layer.find("dl:eq(" + n + ") div.name_cont p.sex span:eq(1)").html(ageCode);
//			$layer.find("dl:eq(" + n + ") div.name_cont p.sex").contents().filter(function(){
//				return (this.nodeType == 3 && this.data.trim() && this.data.trim() != "/");
//			}).before(" / " + areaCode).remove();
			
		}
		, displayApply: function(n){
			var $layer = this._HTMLDom["$layer"];
			var $obj = $layer.find("dl").eq(n);
			var d = this.listData[n]["신청자"];
			
			$obj.find(".offer>span:first").html(d.length).hover(function(){
				if(d.length) _gf.showMemberLayer($(this), d, "left", -30, $(this).height());
			}, function(){
				_gf.closeMemberLayer();
			});
		}
		, displayJoin: function(n){
			var $obj = this._HTMLDom["$layer"].find("dl:eq(" + n + ")");
			var thisClass = this;
			var d = this.listData[n]["참여자"];
			
			$obj.find("ol.person").hover(function(){
				if(d.length) _gf.showMemberLayer($(this), d, "left", -30, $(this).height());
			}, function(){
				_gf.closeMemberLayer();
			}).find("li").filter(":lt(" + d.length + ")").removeClass("off").addClass("on");
		}
		, spinStart: function(){
			var $layer = this.$getLayer();
			var $noresult = this._HTMLDom["$noresult"];
			
			$layer.hide();
			$noresult.hide();

			if($layer.next("div#tmpSpin").length == 0){
				$layer.after("<div id='tmpSpin' style='position:relative; width:700px; height:150px;'></div>");
			}
			$layer.next("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this.$getLayer();
			
			$layer.next("div#tmpSpin").hide().spin(false);
			//$layer.find("dl").show();
		}
		, getKey: function(n){
			return this.listData[n]["키"];
		}
		, isReadAble: function(n){
			if(this.listData[n]["내상태"] == "S" || this.listData[n]["내상태"] == "A" || this.listData[n]["내상태"] == "P") return true;
			
			if(this.listData[n]["비밀방"]) return false;
			
			return true;
		}
		, getPasswd: function(n){
			return this.listData[n]["비밀번호"];
		}
	}
	
	// 상단타이틀
	function clsJoinRoomTitle()
	{
		this._HTMLDom = null;
		this.idx = null;
		
		this._init();
	}
	
	clsJoinRoomTitle.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.depth2_tab");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.find("li img").each(function(){
				var szSrc = $(this).attr("src").replace("_on.gif", ".gif");
				$(this).attr("src", szSrc);
			});
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, display: function(){
			var $layer = this.$getLayer();
			var $obj = $layer.find("li img").eq(this.idx);
			
			var szSrc = $obj.attr("src");
			szSrc = szSrc.substr(0, szSrc.indexOf(".gif")) + "_on.gif";
			
			$obj.attr("src", szSrc);
		}
		, setIdx: function(idx){
			this.idx = idx;
			
			this.display();
		}
		, $getBtns: function(){
			return this.$getLayer().find("li a");
		}
	}
	
	// 검색text
	function clsSearchText()
	{
		this._HTMLDom = null;
		
		this._init();
	}
	
	clsSearchText.prototype = {
		_init: function(){
			var $layer = $(".all_joinroom_search:first").find("input[type=text]:first");
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setValue("");
			$layer.attr("placeholder", "닉네임으로 검색하세요.");
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setValue: function(val){
			this._HTMLDom["$layer"].val(val);
		}
		, getValue: function(){
			return this._HTMLDom["$layer"].val();
		}
	}
	
	// 통신부
	function clsComm()
	{
		this.joinroomList = null;
		this.joinroomRequest = null;
		this.joinroomPageData = null;
		this.bookingTimeList = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			this.joinroomList = [
				{
					"키": null
					, "비밀방": null
					, "작성자": {
						"아이디": null
						, "이미지": null
						, "닉네임": null
					}
					, "작성일": null
					, "작성시": null
					, "코스명": null
					, "홀명": null
					, "참여자": [
						{
							"아이디": null
							, "이미지": null
							, "닉네임": null
						}
					]
					, "신청자": [
						{
							"아이디": null
							, "이미지": null
							, "닉네임": null
						}
					]
					, "내용": null
				}
			];
			
			this.joinroomRequest = {
				"페이지": null
				, "지역": null
				, "시작일": null
				, "종료일": null
				, "시간대": null
				, "검색어": null
				, "내꺼": null
			}
			
			this.joinroomPageData = {
				"전체수": null
			}
		}
		, getJoinroomList: function(){
			var thisClass = this;
			
			var serviceName = "getJoinRoomList";
			if(this.joinroomRequest["내꺼"] == "Y") serviceName = "getMyJoinRoomList";
			document.objGlobal.ajax({
				"sid":"JR001",
				"servicename":serviceName,
				"listcount":this.joinroomRequest["페이지당수"],
				"currpageno":this.joinroomRequest["페이지"]
			}, {
				"bookingTime": this.joinroomRequest["시간대"]
				, "nickName": this.joinroomRequest["검색어"]
				, "location": this.joinroomRequest["지역"]
				, "reserveSt": this.joinroomRequest["시작일"]
				, "reserveEt": this.joinroomRequest["종료일"]
				, "Sort": this.joinroomRequest["정렬코드"]
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "전체수": ret["msp"]["contents"]["@totalcount"]
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						
						res["결과값"].push({
							"키": _util.nvl(obj["joinRoomSeq"])
							, "비밀방": (_util.nvl(obj["openYn"]) == "Y") ? false:true
							, "작성자": {
								"아이디": _util.nvl(obj["bookUserId"])
								, "이미지": _ROOT + "/images/board/default_50_50.gif"
								, "닉네임": _util.nvl(obj["nickName"])
								, "성별": "없음"
								, "나이대": "없음"
								, "지역": "없음"
							}
							, "부킹일": _util.nvl(obj["reserveDate"])
							, "부킹시": _util.nvl(obj["bookingTime"])
							, "요일": _util.nvl(obj["bookingDay"])
							, "골프장코스": _util.nvl(obj["golfClubInfo"])
							, "홀명": _util.nvl(obj["holeInfo"])
							, "조인룸상태코드": _util.nvl(obj["procStatus"])
							, "조인룸상태": _util.nvl(obj["procStatusNm"])
							, "참여자": [
								
							]
							, "신청자": [
								
							]
							, "내용": _util.nvl(obj["ment"])
							, "내상태": (thisClass.joinroomRequest["내꺼"] == "Y") ? "S":_util.nvl(obj["joinStatus"])
							, "비밀번호": _util.nvl(obj["password"])
							, "초대인원": _util.nvl(obj["invitCnt"], 0)
						});
					}
				}
				
				document.objControl.finishJoinroomList(res);
			});
		}
		, setJoinroomRequest: function(key, val){
			if(typeof key == "object"){
				this.joinroomRequest = key;
			}else{
				this.joinroomRequest[key] = val;
			}
		}
		, getJoinroomRequest: function(){
			return this.joinroomRequest;
		}
		, getJoinroomData: function(){
			return this.joinroomList;
		}
		, getJoinroomPageData: function(){
			return this.joinroomPageData;
		}
		// 참여자
		, getJoinList: function(arg){
			var thisClass = this;
			
//			arg = {
//				"조인룸키": null
//			}

			//ajax
			document.objGlobal.ajax({
				"sid":"JR001",
				"servicename":"getMemberList",
				"listcount":10,
				"currpageno":1
			}, {
				"joinRoomSeq": arg["조인룸키"]
			}, function(ret){
				var aList = [];
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						aList.push({
							"아이디": _util.nvl(obj["participantId"])
							, "닉네임": _util.nvl(obj["nickName"])
							, "이미지": _util.nvl(obj["path"]) + _util.nvl(obj["saveFileNm"])
						});
					}
				}

				document.objControl.finishJoinList(arg["조인룸키"], aList);
			});
		}
		// 신청자
		, getApplyList: function(arg){
			var thisClass = this;
			
//			arg = {
//				"조인룸키": null
//			}

			//ajax
			document.objGlobal.ajax({
				"sid":"JR001",
				"servicename":"getApplyMemberList",
				"listcount":10,
				"currpageno":1
			}, {
				"joinRoomSeq": arg["조인룸키"]
			}, function(ret){
				var aList = [];
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						aList.push({
							"아이디": _util.nvl(obj["participantId"])
							, "닉네임": _util.nvl(obj["nickName"])
							, "이미지": _util.nvl(obj["path"]) + _util.nvl(obj["saveFileNm"])
						});
					}
				}
				
				document.objControl.finishApplyList(arg["조인룸키"], aList);
			});
		}
		, getBookingTimeList: function(){
			var thisClass = this;
			
			//ajax
			var serviceName = "getSelectTime";
			document.objGlobal.ajax({
				"sid":"JR001",
				"servicename":serviceName,
				"listcount":10,
				"currpageno":1
			}, {
				
			}, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
				}
				
				thisClass.bookingTimeList = [];
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length){
						items = new Array(items);
					}
					
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){
						var obj = ret["msp"]["contents"]["items"][i];
						
						if(obj["value"]){
							thisClass.bookingTimeList.push({
								"키": obj["value"]
								, "내용": obj["cname"]
							});
						}
					}
				}
				
				document.objControl.finishBookingTimeList(res);
			});
		}
		, getBookingTimeData: function(){
			return this.bookingTimeList;
		}
	}
	
	function clsControl()
	{
		this.objComm = null;
		this.objJoinRoomTitle = null;
		this.objAreaList = null;
		//this.objPeriod = null;
		this.objSDate = null;
		this.objEDate = null;
		this.objTime = null;
		this.objSearchText = null;
		this.objJoinroomOrder = null;
		this.objJoinroomList = null;
		this.objJoinroomPage = null;
		
		this.joinroomIdx = null;
		
		this._permitStart = null;
		
		this.q_my = document.objUtil.getParam("q_my");
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objJoinRoomTitle = new clsJoinRoomTitle();
			this.objAreaList = new clsAreaList();
			//this.objPeriod = new clsPeriod();
			this.objSDate = new clsSDate();
			this.objEDate = new clsEDate();
			this.objTime = new clsTime();
			this.objSearchText = new clsSearchText();
			this.objJoinroomOrder = new clsJoinroomOrder();;
			this.objJoinroomList = new clsJoinroomList();
			this.objJoinroomPage = new document.objGlobal.clsPaging($("#idJoinroomPage"), 10, 10);
			
			document.objUtil.delCookie("joinroomKey", "/");

			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			_gf.spinStart();
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				"지역코드": false
				, "부킹타임코드": false
			}
			
			// 지역코드 리스트
			_gf.getAreaCodeList({
				"콜백": function(ret, listData){
					thisClass.objAreaList.setList(listData);
					
					thisClass._permitStart["지역코드"] = true;
				}
			});
			
			// 부킹시간대 리스트
			this.objComm.getBookingTimeList();
			
			_util.executeThread(function(){
				for(var key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			// 상단 타이틀 클릭
			this.objJoinRoomTitle.$getLayer().on("click", "li", function(){
				var n = thisClass.objJoinRoomTitle.$getLayer().find("li").index($(this));
				// 로그인 체크
				if(n == 1 && !_util.getCookie("member_id")){
					if(!confirm(_LOGIN_TEXT)) return;
					_gf.goLogin();
					return;
				}
				
				document.location.href = document.location.pathname + ((n) ? "?q_my=Y":"")
			});
			
			// 지역 바인드
			this.objAreaList.$getLayer().on("click", "p.default:first, ul li", function(){
				var t = this.tagName.toUpperCase();
				
				switch(t){
					case "P":
						thisClass.objAreaList.toggle();
						//thisClass.objPeriod.hide();
						thisClass.objSDate.hide();
						thisClass.objEDate.hide();
						thisClass.objTime.hide();
						break;
					case "LI":
						var n = thisClass.objAreaList.$getLayer().find("ul li").index($(this));
						thisClass.objAreaList.setIDX(n);
						break;
				}
			});
			
			// 기간별조회
			/*this.objPeriod.$getClick().click(function(){
				thisClass.objPeriod.show();
				thisClass.objHole.hide();
				thisClass.objTime.hide();
			});*/
			
			this.objSDate.$getLayer().click(function(){
				thisClass.objEDate.hide();
				thisClass.objSDate.show();
				thisClass.objAreaList.hide();
				thisClass.objTime.hide();
			});
			
			this.objEDate.$getLayer().click(function(){
				thisClass.objSDate.hide();
				thisClass.objEDate.show();
				thisClass.objAreaList.hide();
				thisClass.objTime.hide();
			});
			
			// 달력 내 닫기 버튼
			/*this.objPeriod.$getClose().click(function(){
				thisClass.objPeriod.hide();
			});*/
			
			$("div#container section#main_content div.selectdate").on("click", "img[alt='close']", function(){
				thisClass.objSDate.hide();
				thisClass.objEDate.hide();
			});
			
			
			// 시간대 바인드
			this.objTime.$getLayer().on("click", "p, ul li", function(){
				var t = this.tagName.toUpperCase();
				
				switch(t){
					case "P":
						thisClass.objTime.toggle();
						thisClass.objAreaList.hide();
						//thisClass.objPeriod.hide();
						thisClass.objSDate.hide();
						thisClass.objEDate.hide();
						break;
					case "LI":
						var n = thisClass.objTime.$getLayer().find("ul li").index($(this));
						thisClass.objTime.setIDX(n);
						break;
				}
			});
			
			// 닉네임검색 엔터
			this.objSearchText.$getLayer().keydown(function(e){
				e = e || event;
				
				if(e.keyCode == 13){
					thisClass.goSearch();
				}
			});
			
			// 검색버튼 클릭
			$("div#container section#main_content").on("click", "input:image[alt='검색'], img[alt='조인룸만들기']", function(){
				var alt = $(this).attr("alt");
				
				switch(alt){
					case "검색":
						thisClass.goSearch();
						break;
					case "조인룸만들기":
						thisClass.goWritePage();
						break;
				}
			});
			
			// 순서
			this.objJoinroomOrder.$getLayer().on("click", "a", function(){
				var n = thisClass.objJoinroomOrder.$getLayer().find("a").index($(this));
				thisClass.objJoinroomOrder.setIdx(n);
				
				thisClass.objJoinroomPage.setPage(1);
				thisClass.goSearch();
			});
			
			// 리스트클릭
			this.objJoinroomList.$getLayer().on("click", "dl", function(){
				thisClass.joinroomIdx = thisClass.objJoinroomList.$getLayer().find("dl").index($(this));
				var isReadable = thisClass.objJoinroomList.isReadAble(thisClass.joinroomIdx);
				
				if(isReadable) {
					thisClass.goViewPage();
				}else{
					var winPasswd = window.open(_ROOT + "/popup/pop_pw.html", "winPasswd", "width=350, height=300, scrollbars=0");
					if(winPasswd) winPasswd.focus();
				}
			});
			
			// 페이지이동
			$(this.objJoinroomPage).bind("evtPageClick", function(){
				var thisPage = this.getPage();
				
				thisClass.objComm.setJoinroomRequest("페이지", thisPage);
				thisClass.objJoinroomList.spinStart();
				thisClass.objComm.getJoinroomList();
			});
		}
		, _start: function(){
			this.objJoinRoomTitle.setIdx((this.q_my == "Y") ? 1:0);
			
			// 로그인 체크
			if(this.q_my == "Y" && !_util.getCookie("member_id")){
				if(!confirm(_LOGIN_TEXT)) history.back();
				_gf.goLogin();
				return;
			}
			
			this.objComm.setJoinroomRequest("내꺼", (this.q_my == "Y") ? "Y":"N");
			
			var q_areaKey = _util.getParam("q_areaKey");
			var q_sDate = _util.getParam("q_sDate");
			var q_eDate = _util.getParam("q_eDate");
			var q_bookingTimeKey = _util.getParam("q_bookingTimeKey");
			var q_nick = _util.getParam("q_nick");
			var q_page = _util.getParam("q_page");
			
			if(q_areaKey){
				this.objAreaList.setKey(q_areaKey);
			}
			
			if(q_sDate){
				this.objSDate.setValue(q_sDate);
			}
			
			if(q_eDate){
				this.objEDate.setValue(q_eDate);
			}
			
			if(q_bookingTimeKey){
				this.objTime.setKey(q_bookingTimeKey);
			}
			
			if(q_nick){
				this.objSearchText.setValue(q_nick);
			}
			
			if(q_page){
				this.objJoinroomPage.setPage(q_page);
			}else{
				this.objJoinroomPage.setPage(1);
			}
			
			this.goSearch();
		}
		// 조인룸 완료
		, finishJoinroomList: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			var thisData = ret["결과값"];
			var pageData = ret["전체수"]
			var thisClass = this;
			
			this.objJoinroomList.spinEnd();
			
			this.objJoinroomList.setList(thisData);
			this.objJoinroomPage.setTotalCount(pageData);
			this.objJoinroomPage.display();
			
			for(var i=0;i<thisData.length;i++){
				// 부킹장정보
				document.objGlobal.getMemberInfo({
					"아이디": thisData[i]["작성자"]["아이디"]
					, "조인룸인덱스": i
					, "콜백": function(ret, infoData, req){
						thisClass.objJoinroomList.setWriterInfo(req["조인룸인덱스"], infoData);
					}
				});
				
				// 참여자
				this.objComm.getJoinList({
					"조인룸키": thisData[i]["키"]
				});
				
				// 신청자
				this.objComm.getApplyList({
					"조인룸키": thisData[i]["키"]
				});
			}
		}
		// 부킹시간리스트 완료
		, finishBookingTimeList: function(ret){
			var thisData = this.objComm.getBookingTimeData();
			
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
				
			this.objTime.setList(thisData);
			
			this._permitStart["부킹타임코드"] = true;
		}
		// 참여자완료
		, finishJoinList: function(key, listData){
			this.objJoinroomList.setJoinList(key, listData);
		}
		// 신청자완료
		, finishApplyList: function(key, listData){
			this.objJoinroomList.setApplyList(key, listData);
		}
		// 상세페이지 이동
		, goViewPage: function(w){
			if(w) w.close();
			
			document.objUtil.setCookie("joinroomKey", this.objJoinroomList.getKey(this.joinroomIdx), null, "/");
			
			var rp = this.objComm.getJoinroomRequest();
			
			var params = {
				"q_my": this.q_my
				, "q_seq": this.objJoinroomList.getKey(this.joinroomIdx)
				, "q_areaKey": rp["지역"]
				, "q_sDate": rp["시작일"]
				, "q_eDate": rp["종료일"]
				, "q_bookingTimeKey": rp["시간대"]
				, "q_nick": rp["검색어"]
				, "q_page": rp["페이지"]
				, "q_sort": rp["정렬코드"]
			}
			
			document.location.href = _ROOT + "/booking/join_room_view01.html" + _util.object2paramString(params);
		}
		// 조인룸 만들기 이동
		, goWritePage: function(q_seq){
			var szLink = "join_room_creat01.html?q_my=" + this.q_my;
			
			var r = _gf.authCheck(_ROOT + "/booking/join_room_creat01.html", true);
			if(!r["결과"]) return;
			
			var rp = this.objComm.getJoinroomRequest();
			q_seq = q_seq || "";
			
			var params = {
				"q_my": this.q_my
				, "q_seq": q_seq
				, "q_areaKey": rp["지역"]
				, "q_sDate": rp["시작일"]
				, "q_eDate": rp["종료일"]
				, "q_bookingTimeKey": rp["시간대"]
				, "q_nick": rp["검색어"]
				, "q_page": rp["페이지"]
				, "q_sort": rp["정렬코드"]
			}
			
			document.location.href = _ROOT + "/booking/join_room_creat01.html" + _util.object2paramString(params);
		}
		, getPasswd: function(){
			return this.objJoinroomList.getPasswd(this.joinroomIdx);
		}
		, goSearch: function(){
			var areaKey = this.objAreaList.getKey();
			var sDate = this.objSDate.getValue();
			var eDate = this.objEDate.getValue();
			var bookingTimeKey = this.objTime.getKey();
			var nick = this.objSearchText.getValue();
			var sort = this.objJoinroomOrder.getKey();
			
			if(Boolean(sDate) ^ Boolean(eDate)){
				alert("기간을 설정하시려면 시작과 종료일을 모두 선택하셔야합니다.");
				return;
			}
			
			this.objJoinroomList.spinStart();
			
			this.objComm.setJoinroomRequest("페이지당수", this.objJoinroomPage.getCountPerPage());
			this.objComm.setJoinroomRequest("페이지", this.objJoinroomPage.getPage());
			this.objComm.setJoinroomRequest("지역", areaKey);
			this.objComm.setJoinroomRequest("시작일", sDate.replace(/-/g, ""));
			this.objComm.setJoinroomRequest("종료일", eDate.replace(/-/g, ""));
			this.objComm.setJoinroomRequest("시간대", bookingTimeKey);
			this.objComm.setJoinroomRequest("검색어", nick);
			this.objComm.setJoinroomRequest("정렬코드", sort);
			this.objComm.getJoinroomList();
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});